package ar.edu.unq.mongodbexample.persistence.commands;

import java.util.ArrayList;
import java.util.List;

import com.mongodb.BasicDBObject;
import com.mongodb.DBCursor;
import com.mongodb.util.JSON;

import flexjson.JSONDeserializer;

/**
 * TODO: description
 */
public class FindCommand<T> extends Command<T> {

    private BasicDBObject query = new BasicDBObject();

    private List<T> result = new ArrayList<T>();

    public FindCommand(final Class clazz) {
        super(clazz);
    }

    public void run() {
        DBCursor cursor = this.getDBCollection().find(this.getQuery());
        while (cursor.hasNext()) {
            String serializedObject = JSON.serialize(cursor.next());
            JSONDeserializer<T> deserializer = new JSONDeserializer<T>();
            this.getResult().add(deserializer.deserialize(serializedObject));
        }
    }

    public BasicDBObject getQuery() {
        return this.query;
    }

    public void setQuery(final BasicDBObject query) {
        this.query = query;
    }

    public List<T> getResult() {
        return this.result;
    }

    public void setResult(final List<T> result) {
        this.result = result;
    }

}
